﻿using log4net;
using server;
using ServerFramework.Tool.Singleton;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;

namespace tcpserver
{

    /// <summary>
    /// 创建队列数据
    /// </summary>
    public class CreateQueueData : Singleton<CreateQueueData>
    {
        private Thread _thread;


        uint time = 0;
        public void Start()
        {
            if(_isRunning)
            {
                return;
            }
            Stop();
            Console.WriteLine("客户端链接成功后，开始产生数据");
            ClearAllData();
          
            while (this._thread != null && this._thread.ThreadState != ThreadState.Aborted)
            {
                Thread.Sleep(1000);
            }
            _isRunning = true;
            time =TestData._GetTimeStamp();
            this._thread = new Thread(new ThreadStart(ProcessHandle));
            this._thread.IsBackground = true;
            this._thread.Start();
           

        }
        private bool _isRunning = false;
        private void ProcessHandle()
        {
            do
            {
                try
                {
                   
                    DataHandle();
                }
                catch (Exception e)
                {

                    Console.WriteLine(e.Message);
                }
                System.Threading.Thread.Sleep(1000);
            }
            while (_isRunning);
        }

        public void Stop()
        {
            Console.WriteLine("执行Stop 创建数据线程关闭");
            _isRunning = false;
            if (this._thread != null)
            {
                this._thread.Abort();
            }

            while (this._thread != null && this._thread.ThreadState != ThreadState.Aborted)
            {
                Thread.Sleep(1000);
            }

            ClearAllData();
        }

        private static ILog logger = log4net.LogManager.GetLogger("tcpserver.CreateQueueData");
        private Queue<List<byte>> dataSources = new Queue<List<byte>>();
        private void DataHandle()
        {
            Console.WriteLine("产生前长度 = "+ dataSources.Count);
            time++;
            logger.Info(time);
            for (int i = 0; i < 1000;)
            {
                byte[] bytes = TestData.GetDataByIndex(i, time);
                dataSources.Enqueue( new List<byte>( bytes));
                i = i + 5;
            }
            Console.WriteLine("产生200 后长度 = " + dataSources.Count);
        }
        /// <summary>
        /// 获取长度
        /// </summary>
        /// <returns></returns>
        public int GetDataLength()
        {

            return dataSources.Count;

        }

        public List<byte> GetSendData(int length)
        {
            if(length> dataSources.Count-1)
            {
                length = dataSources.Count - 1;
            }
            List<byte> result = new List<byte>();
            for(int i=0;i< length;i++)
            {
                List<byte> curentValue = dataSources.Dequeue();
                result.AddRange(curentValue);
            }

            return result;
        }

        public void ClearAllData()
        {
            dataSources.Clear();
        }
    }
}
